﻿using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;
using System.Linq;

namespace Project.Co_DataTransformationPrototype
{
  
    ///// <summary>
    ///// Manage the mouse while dragging a compartment item.
    ///// </summary>
    //public class CompartmentDragMouseAction : MouseAction
    //{
    //    private ModelElement sourceChild;
    //    private EntityShape sourceShape;
    //    private EntityShape targetShape;
    //    private RectangleD sourceCompartmentBounds;
    //    private RectangleD targetCompartmentBounds;

    //    public CompartmentDragMouseAction(ModelElement sourceChildElement, EntityShape sourceParentShape, EntityShape targetParentShape, RectangleD boundsSource, RectangleD boundsTarget)
    //        : base(sourceParentShape.Diagram)
    //    {
    //        sourceChild = sourceChildElement;
    //        sourceShape = sourceParentShape;
    //        targetShape = targetParentShape;
    //        sourceCompartmentBounds = boundsSource; // For cursor.
    //        targetCompartmentBounds = boundsTarget; // For cursor.
    //    }

    //    /// <summary>
    //    /// Call back to the source shape to drop the dragged item.
    //    /// </summary>
    //    /// <param name="e"></param>
    //    protected override void OnMouseUp(DiagramMouseEventArgs e)
    //    {
    //        base.OnMouseUp(e);
    //        sourceShape.DoMouseUp(sourceChild, e);
    //        this.Cancel(e.DiagramClientView);
    //        e.Handled = true;
    //    }

    //    /// <summary>
    //    /// Ideally, this shouldn't happen. This action should only be active
    //    /// while the mouse is still pressed. However, it can happen if you
    //    /// move the mouse rapidly out of the source shape, let go, and then 
    //    /// click somewhere else in the source shape. Yuk.
    //    /// </summary>
    //    /// <param name="e"></param>
    //    protected override void OnMouseDown(DiagramMouseEventArgs e)
    //    {
    //        base.OnMouseDown(e);
    //        this.Cancel(e.DiagramClientView);
    //        e.Handled = false;
    //    }

    //    /// <summary>
    //    /// Display an appropriate cursor while the drag is in progress:
    //    /// Up-down arrow if we are inside the original compartment.
    //    /// No entry if we are elsewhere.
    //    /// </summary>
    //    /// <param name="currentCursor"></param>
    //    /// <param name="diagramClientView"></param>
    //    /// <param name="mousePosition"></param>
    //    /// <returns></returns>
    //    public override System.Windows.Forms.Cursor GetCursor(System.Windows.Forms.Cursor currentCursor, DiagramClientView diagramClientView, PointD mousePosition)
    //    {
    //        // If the cursor is inside the target compartment, show move cursor.
    //        return (targetCompartmentBounds.Contains(mousePosition))
    //            ? System.Windows.Forms.Cursors.NoMove2D
    //            : System.Windows.Forms.Cursors.No;
    //    }
    //}


    /////<summary>
    ///// Override some methods of the compartment shape.
    ///// </summary>
    //public partial class EntityShape
    //{
    //    /// <summary>
    //    /// Model element that is being dragged.
    //    /// </summary>
    //    private static EntityAttribute dragStartAttribute = null;
    //    /// <summary>
    //    /// Absolute bounds of the compartment, used to set the cursor.
    //    /// </summary>
    //    private static RectangleD sourceCompartmentBounds;
    //    private static RectangleD targetCompartmentBounds;

    //    //private static ExampleElement dragTarget = null; // org
    //    private static Entity dragSourceElement = null;
    //    private static Entity dragTargetElement = null;

    //    /// <summary>
    //    /// Attach mouse listeners to the compartments for the shape.
    //    /// This is called once per compartment shape.
    //    /// The base method creates the compartments for this shape.
    //    /// </summary>
    //    public override void EnsureCompartments()
    //    {
    //        base.EnsureCompartments();
    //        foreach (Compartment compartment in this.NestedChildShapes.OfType<Compartment>())
    //        {
    //            compartment.MouseDown += new DiagramMouseEventHandler(compartment_MouseDown);
    //        }
    //    }

    //    /// <summary>
    //    /// Remember which item the mouse was dragged from.
    //    /// We don't create an Action immediately, as this would inhibit the
    //    /// inline text editing feature. Instead, we just remember the details
    //    /// and will create an Action when/if the mouse moves off this list item.
    //    /// </summary>
    //    /// <param name="sender"></param>
    //    /// <param name="e"></param>
    //    void compartment_MouseDown(object sender, DiagramMouseEventArgs e)
    //    {
    //        dragStartAttribute = e.HitDiagramItem.RepresentedElements.OfType<EntityAttribute>().FirstOrDefault();
    //        if (dragStartAttribute != null)
    //        {
    //            if (dragStartAttribute.isPrimaryKey)
    //            {
    //                dragSourceElement = dragStartAttribute.Entity;
    //                sourceCompartmentBounds = e.HitDiagramItem.Shape.AbsoluteBoundingBox;
    //            }
    //            else
    //                dragStartAttribute = null;
    //        }
    //    }

    //    public override void OnMouseEnter(DiagramPointEventArgs e)
    //    {
    //        base.OnMouseEnter(e);
    //        EntityShape targetShape = e.HitDiagramItem.RepresentedElements.OfType<EntityShape>().FirstOrDefault();
    //        if (dragStartAttribute != null && targetShape != null)
    //        {
    //            if ((targetShape.ModelElement as Entity) != null && dragSourceElement != (targetShape.ModelElement as Entity))
    //            {
    //                dragTargetElement = (targetShape.ModelElement as Entity);
    //                targetCompartmentBounds = e.HitDiagramItem.Shape.AbsoluteBoundingBox;
    //                e.DiagramClientView.ActiveMouseAction = new CompartmentDragMouseAction(dragStartAttribute, PresentationViewsSubject.GetPresentation(dragSourceElement)[0] as EntityShape, this, sourceCompartmentBounds, targetCompartmentBounds);
    //            }
    //        }
    //    }

    //    public override void OnMouseLeave(DiagramPointEventArgs e)
    //    {
    //        base.OnMouseLeave(e);

    //        if (dragTargetElement != null)
    //        {
    //            // Re-set Target
    //            dragTargetElement = null;
    //        }
    //    }

    //    internal void DoMouseUp(ModelElement sourceChild, DiagramMouseEventArgs e)
    //    {
    //        if (dragSourceElement != null && dragTargetElement != null)
    //        {
    //            EntityAssociationBuilder.Connect(dragSourceElement, dragTargetElement);
    //        }
    //        dragStartAttribute = null;
    //        dragSourceElement = dragTargetElement = null;
    //    }
    //}

}
